package com.hym.vue_cli_demo1.controller;


import com.hym.vue_cli_demo1.entity.User;
import com.hym.vue_cli_demo1.service.UserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * @Auther: hyming
     * Description: 查询所有用户信息
     * @Date :21:27 2020/11/25
     * @Return:
     * @Param:  * @param null
     */

    @GetMapping("/findAll")
    public Map<String,Object> findAll(){
        Map<String,Object> map = new HashMap<>();
        List<User> list = new ArrayList<>();
        try {
            list = userService.findAll();
            map.put("data",list);
            map.put("msg","成功");
            map.put("status",true);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","失败");
            map.put("status",false);
        }
        return map;
    }
    @PostMapping("/insertUser")
    public Map<String,Object> insertUser(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        try {
            System.out.println("user:"+user);
            Integer count = userService.insertUser(user);
            map.put("data",count);
            map.put("msg","成功");
            map.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","失败");
            map.put("status",false);
        }
        return map;
    }
    @GetMapping("/deleteUser")
    public Map<String,Object> deleteUser(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            Integer count = userService.deleteUser(id);
            map.put("data",count);
            map.put("msg","成功");
            map.put("status",true);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","失败");
            map.put("status",false);
        }
        return map;
    }
    @PostMapping("/updateUser")
    public Map<String,Object> updateUser(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        try {
            Integer count = userService.updateUser(user);
            map.put("data",count);
            map.put("msg","成功");
            map.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","失败");
            map.put("status",false);
        }
        return map;
    }
    @GetMapping("/findOne")
    public  User findOne(@Param("id") Integer id){
        /*Map<String,Object> map = new HashMap<>();
        try {
            List<User> list = userService.findOne(id);
            map.put("data",list);
            map.put("msg","成功");
            map.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","失败");
            map.put("status",false);
        }
        return map;*/
        return userService.findOne(id);
    }
    @GetMapping("/search")
    public Map<String,Object> search(@Param("name") String name,@Param("phone") String phone){

        Map<String,Object> map = new HashMap<>();

        try {
            List<User> user = userService.search(name,phone);
            map.put("data",user);
            map.put("msg","成功");
            map.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","失败");
            map.put("status",false);
        }
        return map;

    }
}
